using System.Collections.Generic;
using System.Linq;
using IRepository;
using IServices;
using SqlSugar;
using YL.Core.Entity;
using YL.Utils.Extensions;
using YL.Utils.Table;

namespace Services
{
    public class Whr_commentServices : BaseServices<Whr_comment>, IWhr_commentServices
    {
        private readonly SqlSugarClient _client;

        private readonly IWhr_commentRepository _commentRepository;

        public Whr_commentServices(SqlSugarClient client, IWhr_commentRepository repository) : base(repository)
        {
            _client = client;
            _commentRepository = repository;
        }
        
        public (int, List<Whr_comment>) PageList(Bootstrap.BootstrapParams bootstrap, int courseId)
        {
            var totalNumber = 0;
            var query = _client.Queryable<Whr_comment>().Where(it => it.CourseId == courseId).Mapper((it, cache) =>
            {
                var allUsers = cache.GetListByPrimaryKeys<Whr_user>(u => u.UserId);
                var user = allUsers.FirstOrDefault(u => u.UserId == it.UserId);
                it.Nickname = user.Nickname.IsNull() ? "" : user.Nickname;
            }).Mapper(it => it.CommentImages, it => it.CommentImages.First().CommentId);
            var result = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return (totalNumber, result);
        }
    }
}